package com.project.service.impl;

import com.project.bean.SellCarBean;
import com.project.mapper.ISellCarMapper;
import com.project.service.ISellCarService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class SellCarServiceImpl implements ISellCarService {
    @Autowired
    private ISellCarMapper sellCarMapper;
    @Override
    public List<SellCarBean> fidById(Integer userId) {
        return sellCarMapper.findByUserId(userId);
    }

    @Override
    public void add(String productId, Integer userId) {
        SellCarBean sellCarBean = sellCarMapper.findByProductId(productId, userId);
        if (sellCarBean != null){
            sellCarBean.setNum(sellCarBean.getNum() + 1);
            sellCarMapper.updateById(sellCarBean);
        }
        else {
            sellCarBean = new SellCarBean();
            sellCarBean.setNum(1);
            sellCarBean.setProductId(productId);
            sellCarBean.setUserId(userId);

            sellCarMapper.insert(sellCarBean);
        }
    }

    @Override
    public void remove(Integer carId) {
        sellCarMapper.deleteById(carId);
    }

    @Override
    public void updateNum(Integer carId, Integer num) {
        SellCarBean sellCarBean = new SellCarBean();
        sellCarBean.setId(carId);
        sellCarBean.setNum(num);
        sellCarMapper.updateById(sellCarBean);
    }

    @Override
    public Integer getTotalPrice(Integer userId) {
        return sellCarMapper.getTotalPrice(userId);
    }
}
